
* Clean up
clear all

* Set working directory: commented; please set your own working directory *
* cd "~/Desktop/CPS replication/"

********************************************************************************
************************* STUDY 1: COMPARATIVE EVIDENCE ************************
********************************************************************************

use study1, clear

*******************
** MAIN ANALYSES **
*******************

* Generate an excel file to store the findings
putexcel set df_main_plot.xlsx, replace

** Non-parametric approach **
* Comumn 1-3: Radical right parties, unweighted
rdrobust reported_vote performance if rad_right == 1, fuzzy(parl) vce(cluster country_code) all

	* Save results to Excel

	* Conventional model
	local coef1 = e(tau_cl)
	local se1 = e(se_tau_cl)
	
	putexcel A1 = `coef1'
	putexcel B1 = `se1' 
	putexcel C1 = "Non-parametric approach (conventional)"	 
	putexcel D1 = "Unweighted"
	putexcel E1 = "Radical right"

	* Bias-corrected model
	local coef2 = e(tau_bc)
	local se2 = e(se_tau_cl)
	
	putexcel A2 = `coef2'
	putexcel B2 = `se2'	 
	putexcel C2 = "Non-parametric approach (bias-corrected)"	 
	putexcel D2 = "Unweighted"
	putexcel E2 = "Radical right"

	* Robust model
	local coef3 = e(tau_bc)
	local se3 = e(se_tau_rb)
	
	putexcel A3 = `coef3'
	putexcel B3 = `se3'	 	
	putexcel C3 = "Non-parametric approach (robust)"	 
	putexcel D3 = "Unweighted"
	putexcel E3 = "Radical right"

* Comumn 4-6: Radical right parties, weighted
rdrobust reported_vote_w performance if rad_right == 1, fuzzy(parl) vce(cluster country_code) all

	* Save results to Excel
	
	* Conventional model
	local coef4 = e(tau_cl)
	local se4 = e(se_tau_cl)
	
	putexcel A4 = `coef4'
	putexcel B4 = `se4' 	 
	putexcel C4 = "Non-parametric approach (conventional)"	 
	putexcel D4 = "Sociodem weights"
	putexcel E4 = "Radical right"

	* Bias-corrected model
	local coef5 = e(tau_bc)
	local se5 = e(se_tau_cl)
	
	putexcel A5 = `coef5'
	putexcel B5 = `se5'	  
	putexcel C5 = "Non-parametric approach (bias-corrected)"	 
	putexcel D5 = "Sociodem weights"
	putexcel E5 = "Radical right"

	* Robust model
	local coef6 = e(tau_bc)
	local se6 = e(se_tau_rb)
	
	putexcel A6 = `coef6'
	putexcel B6 = `se6'	 	 
	putexcel C6 = "Non-parametric approach (robust)"	 
	putexcel D6 = "Sociodem weights"
	putexcel E6 = "Radical right"

** Parametric approach **
* Column 7: Radical right parties, unweighted
regr reported_vote parl##c.performance  ///
if rad_right == 1, cluster(country_code)
	
	* Save results to Excel
	local coef7 = el(r(table),1,2)
	local se7 = el(r(table),2,2)

	putexcel A7 = `coef7'
	putexcel B7 = `se7'	 
	putexcel C7 = "Parametric approach"	 
	putexcel D7 = "Unweighted"
	putexcel E7 = "Radical right"

* Column 8: Radical right parties, weighted
regr reported_vote_w parl##c.performance ///
if rad_right == 1, cluster(country_code)

	* Save results to Excel
	local coef8 = el(r(table),1,2)
	local se8 = el(r(table),2,2)

	putexcel A8 = `coef8'
	putexcel B8 = `se8' 
	putexcel C8 = "Parametric approach"	 
	putexcel D8 = "Sociodem weights"
	putexcel E8 = "Radical right"

** PLACEBO USING CENTER-RIGHT PARTIES
** Non-parametric approach **
** Column 1-3: Centre right parties, unweighted
rdrobust reported_vote performance ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & rad_right == 0, fuzzy (parl) vce(cluster country_code) all

	* Save results to Excel

	* Conventional model
	local coef9 = e(tau_cl)
	local se9 = e(se_tau_cl)
	
	putexcel A9 = `coef9'
	putexcel B9 = `se9'  
	putexcel C9 = "Non-parametric approach (conventional)"	 
	putexcel D9 = "Unweighted"
	putexcel E9 = "Placebo (Center Right)"

	* Bias-corrected model
	local coef10 = e(tau_bc)
	local se10 = e(se_tau_cl)
	
	putexcel A10 = `coef10'
	putexcel B10 = `se10'	
	putexcel C10 = "Non-parametric approach (bias-corrected)"	 
	putexcel D10 = "Unweighted"
	putexcel E10 = "Placebo (Center Right)"

	* Robust model
	local coef11 = e(tau_bc)
	local se11 = e(se_tau_rb)
	
	putexcel A11 = `coef11'
	putexcel B11 = `se11'	
	putexcel C11 = "Non-parametric approach (robust)"	 
	putexcel D11 = "Unweighted"
	putexcel E11 = "Placebo (Center Right)"

** Column 4-6: Centre right parties, weighted
rdrobust reported_vote_w performance ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & rad_right == 0, fuzzy (parl) vce(cluster country_code) all

	* Save results to Excel
	
	* Conventional model
	local coef12 = e(tau_cl)
	local se12 = e(se_tau_cl)
	
	putexcel A12 = `coef12'
	putexcel B12 = `se12'   
	putexcel C12 = "Non-parametric approach (conventional)"	 
	putexcel D12 = "Sociodem weights"
	putexcel E12 = "Placebo (Center Right)"

	* Bias-corrected model
	local coef13 = e(tau_bc)
	local se13 = e(se_tau_cl)
	
	putexcel A13 = `coef13'
	putexcel B13 = `se13'
	putexcel C13 = "Non-parametric approach (bias-corrected)"	 
	putexcel D13 = "Sociodem weights"
	putexcel E13 = "Placebo (Center Right)"

	* Robust model
	local coef14 = e(tau_bc)
	local se14 = e(se_tau_rb)
	
	putexcel A14 = `coef14'
	putexcel B14 = `se14'	
	putexcel C14 = "Non-parametric approach (robust)"	 
	putexcel D14 = "Sociodem weights"
	putexcel E14 = "Placebo (Center Right)"

** Parametric approach **
* Column 7: Centre right parties, unweighted
regr reported_vote parl##c.performance ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, cluster(country_code)

	* Save results to Excel
	local coef15 = el(r(table),1,2)
	local se15 = el(r(table),2,2)

	putexcel A15 = `coef15'
	putexcel B15 = `se15'
	putexcel C15 = "Parametric approach"	 
	putexcel D15 = "Unweighted"
	putexcel E15 = "Placebo (Center Right)"
	
* Column 8: Centre right parties, weighted
regr reported_vote_w parl##c.performance ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, cluster(country_code)

	* Save results to Excel
	local coef16 = el(r(table),1,2)
	local se16 = el(r(table),2,2)

	putexcel A16 = `coef16'
	putexcel B16 = `se16'
	putexcel C16 = "Parametric approach"	 
	putexcel D16 = "Sociodem weights"
	putexcel E16 = "Placebo (Center Right)"
	
***********************************************
** LOOKING AT HOW LONG LASTING THE EFFECT IS **
***********************************************

* For parties in the parliament
use study1_daily, clear

* Creating dummies for first 80 days after election
ta dse if dse < 81, gen(dse)

regr radright_parl dse1-dse80 i.election_code [weight = dem_weight], cluster(election_code)
predict predvalue
predict se, stdp
collapse predvalue se parl inparl_before, by(dse)
gen ci_upper = predvalue + 1.96 * se
gen ci_lower = predvalue - 1.96 * se
drop parl
gen parl = 1

saveold predictparl, replace version(12)

* For parties outside the parliament
use study1_daily, clear

* Creating dummies for first 80 days after election
ta dse if dse < 81, gen(dse)

regr radright_noparl dse1-dse77 i.election_code [weight = dem_weight], cluster(election_code)
predict predvalue
predict se, stdp
collapse predvalue se parl inparl_before, by(dse)
gen ci_upper = predvalue + 1.96 * se
gen ci_lower = predvalue - 1.96 * se
drop parl
gen parl = 0

saveold predictnoparl, replace version(12)

append using predictparl

saveold predictboth, replace version(12)

***********************************************
** ROBUSTNESS CHECKS AND ADDITIONAL ANALYSES **
***********************************************

use study1, clear

** Evidence of the underreport of vote for RRPs in CSES **
ttest vote_cses == official_vote if rad_right == 1

** Evidence that this underreport has not necessarily changed across time
regr reported_vote i.year if rad_right == 1, cluster(country_code)
predict repvote_pred
predict repvote_se, stdp
collapse repvote_pred repvote_se, by(year)
gen ci_upper = repvote_pred + 1.96 * repvote_se
gen ci_lower = repvote_pred - 1.96 * repvote_se

saveold repvote_pred.dta, version(12) replace

*************
** Checking how many polynomials to use in each parametric approach **
*************

estimates clear 
use study1, clear

* Radical right parties, unweighted
regr reported_vote parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store radright_unweighted1

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1, cluster(country_code)
est store radright_unweighted2

regr reported_vote parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1, cluster(country_code)
est store radright_unweighted3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1, cluster(country_code)
est store radright_unweighted4

estimates stats radright_unweighted1 radright_unweighted2 radright_unweighted3 radright_unweighted4

* Radical right parties, weighted
regr reported_vote_w parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store radright_weighted1

regr reported_vote_w c.performance##parl c.performance_sq##parl ///
if rad_right == 1, cluster(country_code)
est store radright_weighted2

regr reported_vote_w c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1, cluster(country_code)
est store radright_weighted3

regr reported_vote_w c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1, cluster(country_code)
est store radright_weighted4

estimates stats radright_weighted1 radright_weighted2 radright_weighted3 radright_weighted4

* Center right parties, unweighted
regr reported_vote parl##c.performance ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, ///
cluster (country_code) 
est store centreright_unweighted1

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, ///
cluster (country_code) 
est store centreright_unweighted2

regr reported_vote parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, ///
cluster (country_code) 
est store centreright_unweighted3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, ///
cluster (country_code) 
est store centreright_unweighted4

estimates stats centreright_unweighted1 centreright_unweighted2 centreright_unweighted3 centreright_unweighted4

* Center right parties, weighted
regr reported_vote_w parl##c.performance ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, ///
cluster (country_code) 
est store centreright_weighted1

regr reported_vote_w parl c.performance##parl c.performance_sq##parl ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, ///
cluster (country_code) 
est store centreright_weighted2

regr reported_vote_w parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, ///
cluster (country_code) 
est store centreright_weighted3

regr reported_vote_w parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if ches_genlr >= 5.5 & ches_genlr <= 8 & ches_genlr != . & rad_right == 0, ///
cluster (country_code) 
est store centreright_weighted4

estimates stats centreright_weighted1 centreright_weighted2 centreright_weighted3 centreright_weighted4

*********
** Model using only the most voted RRP in each election
*********
** Non-parametric approach
rdrobust reported_vote performance if rad_right == 1 & max_dummy == 1, fuzzy (parl) vce(cluster country_code) all

** Parametric approach
* First I check what the optimal number of polynomials is
regr reported_vote parl##c.performance ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store radright_mostvoted1

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store radright_mostvoted2

regr reported_vote parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store radright_mostvoted3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store radright_mostvoted4

estimates stats radright_mostvoted1 radright_mostvoted2 radright_mostvoted3 radright_mostvoted4

* Then I run the analyses
regr reported_vote parl##c.performance parl##c.performance_sq parl##c.performance3 /// 
if rad_right == 1 & max_dummy == 1, cluster(country_code)

************************
** CHECKING WHAT RRP VOTERS REPLY WHEN THEY DO NOT REPORT THEIR PRIVATE PREFERENCES **
************************

** Non-parametric approach **
* Center right parties
rdrobust reported_vote_total_cr performance if rad_right == 1 & max_dummy == 1, fuzzy(parl) vce(cluster country_code) all

* Center left parties
rdrobust reported_vote_total_cl performance if rad_right == 1 & max_dummy == 1, fuzzy(parl) vce(cluster country_code) all

* All left parties
rdrobust reported_vote_total_l performance if rad_right == 1 & max_dummy == 1, fuzzy(parl) vce(cluster country_code) all

* Turnout
rdrobust reported_vote_turnout performance if rad_right == 1 & max_dummy == 1, fuzzy(parl) vce(cluster country_code) all

* DK/NA
rdrobust dkna performance if rad_right == 1 & max_dummy == 1, fuzzy(parl) vce(cluster country_code) all

** Parametric approach: center right
* First check how many polynomials to use
regr reported_vote_total_cr parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store movers_cr1

regr reported_vote_total_cr parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1  & max_dummy == 1, cluster(country_code)
est store movers_cr2

regr reported_vote_total_cr parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1  & max_dummy == 1, cluster(country_code)
est store movers_cr3

regr reported_vote_total_cr parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_cr4

estimates stats movers_cr1 movers_cr2 movers_cr3 movers_cr4
* Then run the analyses 
regr reported_vote_total_cr parl##c.performance ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)

** Parametric approach: center left
* First check how many polynomials to use
regr reported_vote_total_cl parl##c.performance ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_cl1

regr reported_vote_total_cl parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_cl2

regr reported_vote_total_cl parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_cl3

regr reported_vote_total_cl parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_cl4

estimates stats movers_cl1 movers_cl2 movers_cl3 movers_cl4

* Then run the analyses 
regr reported_vote_total_cl parl##c.performance ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)

** Parametric approach: all left parties
* First check how many polynomials to use
regr reported_vote_total_l parl##c.performance ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_l1

regr reported_vote_total_l parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_l2

regr reported_vote_total_l parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_l3

regr reported_vote_total_l parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_l4

estimates stats movers_l1 movers_l2 movers_l3 movers_l4

* Then run the analyses (one polynomial)
regr reported_vote_total_l parl c.performance##parl ///
if rad_right == 1, cluster(country_code)


** Parametric approach: turnout
* First check how many polynomials to use
regr reported_vote_turnout parl##c.performance ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_turnout1

regr reported_vote_turnout parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_turnout2

regr reported_vote_turnout parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_turnout3

regr reported_vote_turnout parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_turnout4

estimates stats movers_turnout1 movers_turnout2 movers_turnout3 movers_turnout4

* Then run the analyses (two polynomials)
regr reported_vote_total_l parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)

** Parametric approach: DK/NA
* First check how many polynomials to use
regr dkna parl##c.performance ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_dkna1

regr dkna parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_dkna2

regr dkna parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_dkna3

regr dkna parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)
est store movers_dkna4

estimates stats movers_dkna1 movers_dkna2 movers_dkna3 movers_dkna4

* Then run the analyses (one polynomial)
regr dkna parl c.performance##parl ///
if rad_right == 1 & max_dummy == 1, cluster(country_code)


******
** Analyses with socio demographic variables as dependent variable **
******

use study1, clear

** Non parametric approach
* Median age *
rdrobust mage_diff performance if rad_right == 1, fuzzy (parl) vce(cluster country_code) all

* Percentage of women *
rdrobust womenp_diff performance if rad_right == 1, fuzzy (parl) vce(cluster country_code) all

* Household income *
rdrobust householdincome_mean performance if rad_right == 1, fuzzy (parl) vce(cluster country_code) all

** Parametric approach
* First I check the optimal number of polynomials in each of these models
* Median age
regr mage_diff parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store mage1

regr mage_diff parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1, cluster(country_code)
est store mage2

regr mage_diff parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1, cluster(country_code)
est store mage3

regr mage_diff parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1, cluster(country_code)
est store mage4

estimates stats mage1 mage2 mage3 mage4

* Percentage of women
regr womenp_diff parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store womenp_diff1

regr womenp_diff parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1, cluster(country_code)
est store womenp_diff2

regr womenp_diff parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1, cluster(country_code)
est store womenp_diff3

regr womenp_diff parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1, cluster(country_code)
est store womenp_diff4

estimates stats womenp_diff1 womenp_diff2 womenp_diff3 womenp_diff4

* Household income
regr householdincome_mean parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store hinc1

regr householdincome_mean parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1, cluster(country_code)
est store hinc2

regr householdincome_mean parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1, cluster(country_code)
est store hinc3

regr householdincome_mean parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1, cluster(country_code)
est store hinc4

estimates stats hinc1 hinc2 hinc3 hinc4

** Then I run the analyses
* Median age *
regr mage_diff parl##c.performance ///
if rad_right == 1, cluster(country_code)

* Percentage of women *
regr womenp_diff parl##c.performance ///
if rad_right == 1, cluster(country_code)

* Household income *
regr householdincome_mean parl##c.performance /// 
if rad_right == 1, cluster(country_code)

**********
** Looking at differences in the left-right placement of RRPs in and out of parliament
**********

* Non parametric
rdrobust ches_genlr performance if rad_right == 1, fuzzy (parl) vce(cluster country_code) all

* Parametric
* First I check the optimal number of polynomials
regr ches_genlr parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store leftright1

regr ches_genlr parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1, cluster(country_code)
est store leftright2

regr ches_genlr parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1, cluster(country_code)
est store leftright3

regr ches_genlr parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1, cluster(country_code)
est store leftright4

estimates stats leftright1 leftright2 leftright3 leftright4

* Then I run the analyses
regr ches_genlr parl##c.performance /// 
if rad_right == 1, cluster(country_code)

*******
** Analyses controlling for district magnitude
*******

rdrobust reported_vote performance if rad_right == 1, fuzzy(parl) vce(cluster country_code) all covs(agv_district_magnitude)

* First I check the optimal number of polynomials
regr reported_vote parl##c.performance agv_district_magnitude ///
if rad_right == 1, cluster(country_code)
est store magnitude1

regr reported_vote parl c.performance##parl c.performance_sq##parl agv_district_magnitude ///
if rad_right == 1, cluster(country_code)
est store magnitude2

regr reported_vote parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
agv_district_magnitude if rad_right == 1, cluster(country_code)
est store magnitude3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl agv_district_magnitude ///
if rad_right == 1, cluster(country_code)
est store magnitude4

estimates stats magnitude1 magnitude2 magnitude3 magnitude4

* Then I run the analyses
regr reported_vote parl##c.performance agv_district_magnitude ///
if rad_right == 1, cluster(country_code)

*******
** Jackknife estimates
*******

* Generate an excel file to store the findings
putexcel set jackknives.xlsx, replace

* Generate a number to identify the iterations, so that then we can merge the ///
* file with results and the one with first stage f-statistic
local i = 1

* Non parametric: conventional approach
forvalues x= 1/21{
rdrobust reported_vote performance if rad_right == 1 & cntr`x' != 1 ///
, fuzzy (parl) vce(cluster country_code) all

	local coef = e(tau_cl)
	local se = e(se_tau_cl)
	
	putexcel A`i' = `i'
	putexcel B`i' = `coef'
	putexcel C`i' = `se'	 
	putexcel D`i' = "Non-parametric approach (conventional procedure)"	 
	putexcel E`i' = 3 
  
	local ++i
}

* Non parametric: bias-corrected approach
forvalues x= 1/21{
rdrobust reported_vote performance if rad_right == 1 & cntr`x' != 1 ///
, fuzzy (parl) vce(cluster country_code) all

 
	local coef = e(tau_bc)
	
	local se = e(se_tau_cl) 
	
	putexcel A`i' = `i'
	putexcel B`i' = `coef'
	putexcel C`i' = `se'	 
	putexcel D`i' = "Non-parametric approach (bias-corrected procedure)"	 
	putexcel E`i' = 4 
 
	local ++i
}


* Non parametric: robust approach
forvalues x= 1/21{
rdrobust reported_vote performance if rad_right == 1 & cntr`x' != 1 ///
, fuzzy (parl) vce(cluster country_code) all

	local coef = e(tau_bc)
	local se = e(se_tau_rb)
	
	putexcel A`i' = `i'
	putexcel B`i' = `coef'
	putexcel C`i' = `se'	 
	putexcel D`i' = "Non-parametric approach (robust procedure)"
	putexcel E`i' = 2 
 
	local ++i
}


* Parametric (one polynomial)

forvalues x= 1/21{
regr reported_vote parl##c.performance ///
if rad_right == 1 & cntr`x' != 1, cluster(country_code)

	local parametric_coef = _b[1.parl]
	local parametric_se = _se[1.parl]
	
	 putexcel A`i' = `i'
	 putexcel B`i' = `parametric_coef'	 
	 putexcel C`i' = `parametric_se'
	 putexcel D`i' = "Parametric approach (One polynomial)"	 
	 putexcel E`i' = 1	 

	local ++i

}

** Analyses excluding parties on which there is discrepancy
* 1. excluding parties not coded as RRP by Mudde
* a. Non-parametric approach
rdrobust reported_vote performance if rad_right == 1 & mudde_not == 0, fuzzy(parl) vce(cluster country_code) all

* b. parametric approach: checking how many polynomials are optimal
regr reported_vote parl##c.performance ///
if rad_right == 1 & mudde_not == 0, cluster(country_code)
est store mudde1

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1 & mudde_not == 0, cluster(country_code)
est store mudde2

regr reported_vote parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1 & mudde_not == 0, cluster(country_code)
est store mudde3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1 & mudde_not == 0, cluster(country_code)
est store mudde4

estimates stats mudde1 mudde2 mudde3 mudde4

* Running the analyses
regr reported_vote parl##c.performance  ///
if rad_right == 1  & mudde_not == 0, cluster(country_code)

* 2. excluding parties not coded as RRP by Norris
* a. Non-parametric approach
rdrobust reported_vote performance if rad_right == 1 & norris_not == 0, fuzzy(parl) vce(cluster country_code) all

* b. parametric approach: checking how many polynomials are optimal
regr reported_vote parl##c.performance ///
if rad_right == 1 & norris_not == 0, cluster(country_code)
est store norris1

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1 & norris_not == 0, cluster(country_code)
est store norris2

regr reported_vote parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1 & norris_not == 0, cluster(country_code)
est store norris3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1 & norris_not == 0, cluster(country_code)
est store norris4

estimates stats norris1 norris2 norris3 norris4

* Running the analyses
regr reported_vote parl##c.performance  ///
if rad_right == 1  & norris_not == 0, cluster(country_code)

**********
** Model with the vote share in CSES as the dependent variable
**********

** Non-parametric approach
rdrobust vote_cses performance if rad_right == 1, fuzzy(parl) vce(cluster country_code) all

** Parametric approach
* First I check the optimal number of polynomials
regr vote_cses parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store cses1

regr vote_cses parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1, cluster(country_code)
est store cses2

regr vote_cses parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1, cluster(country_code)
est store cses3

regr vote_cses parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1, cluster(country_code)
est store cses4

estimates stats cses1 cses2 cses3 cses4

* Then I run the analyses
regr vote_cses parl##c.performance ///
if rad_right == 1, cluster(country_code)

*********
** Model using the total reported vote as dependent variable
*********
** Non-parametric approach
rdrobust overall_reported_vote performance if rad_right == 1, fuzzy (parl) vce(cluster country_code) all

** Parametric approach
* First I check what the optimal number of polynomials is
regr overall_reported_vote parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store altdv1

regr overall_reported_vote parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1, cluster(country_code)
est store altdv2

regr overall_reported_vote parl c.performance##parl c.performance_sq##parl c.performance3##parl ///
if rad_right == 1, cluster(country_code)
est store altdv3

regr overall_reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1, cluster(country_code)
est store altdv4

estimates stats altdv1 altdv2 altdv3 altdv4

* Then I run the analyses
regr overall_reported_vote parl##c.performance parl##c.performance_sq parl##c.performance3 /// 
if rad_right == 1, cluster(country_code)

*******
** Including country fixed effects
*******
** Non-parametric approach with RDEstimate package: see R script

** Parametric approach
* First I check what the optimal number of polynomials is
regr reported_vote parl##c.performance  i.country_code ///
if rad_right == 1, cluster(country_code) 
est store countryfe1

regr reported_vote parl c.performance##parl c.performance_sq##parl  i.country_code ///
if rad_right == 1, cluster(country_code)
est store countryfe2

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl  i.country_code ///
if rad_right == 1, cluster(country_code)
est store countryfe3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl i.country_code ///
if rad_right == 1, cluster(country_code)
est store countryfe4

estimates stats countryfe1 countryfe2 countryfe3 countryfe4

* Then I run the analyses
regr reported_vote parl##c.performance i.country_code ///
if rad_right == 1, cluster(country_code)

*******
** Including wave fixed effects
*******

** Non-parametric approach
rdrobust reported_vote performance if rad_right == 1, fuzzy(parl) covs(wave1 wave2 wave3 wave4) vce(cluster country_code) all

** Parametric approach
* First I check what the optimal number of polynomials is
regr reported_vote parl##c.performance  wave1 wave2 wave3 wave4 ///
if rad_right == 1, cluster(country_code) 
est store wavefe1

regr reported_vote parl c.performance##parl c.performance_sq##parl  wave1 wave2 wave3 wave4 ///
if rad_right == 1, cluster(country_code)
est store wavefe2

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl wave1 wave2 wave3 wave4 ///
if rad_right == 1, cluster(country_code)
est store wavefe3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl wave1 wave2 wave3 wave4 ///
if rad_right == 1, cluster(country_code)
est store wavefe4

estimates stats wavefe1 wavefe2 wavefe3 wavefe4

* Then I run the analyses
regr reported_vote parl##c.performance wave1 wave2 wave3 wave4 ///  
if rad_right == 1, cluster(country_code)

*******
** Including control for the vote for RRPs
*******

** Non-parametric approach
rdrobust reported_vote performance if rad_right == 1, fuzzy(parl) covs(sum_rrp_vote) ///
vce(cluster country_code) all

** Parametric approach
* First I check what the optimal number of polynomials is
regr reported_vote parl##c.performance  sum_rrp_vote ///
if rad_right == 1, cluster(country_code) 
est store inparl1

regr reported_vote parl c.performance##parl c.performance_sq##parl  sum_rrp_vote ///
if rad_right == 1, cluster(country_code)
est store inparl2

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl sum_rrp_vote ///
if rad_right == 1, cluster(country_code)
est store inparl3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl sum_rrp_vote ///
if rad_right == 1, cluster(country_code)
est store inparl4

estimates stats inparl1 inparl2 inparl3 inparl4

* Then I run the analyses
regr reported_vote parl##c.performance sum_rrp_vote ///  
if rad_right == 1, cluster(country_code)

** Model using the sum reported

********
** Model assuming AfD as a radical right party alteady in 2013
********

replace rad_right = 1 if partyyr == "AfD_GER13"

** Non-parametric
rdrobust reported_vote performance if rad_right == 1, fuzzy(parl) vce(cluster country_code) all

** Parametric
* First I check what the optimal number of polynomials is
regr reported_vote parl##c.performance ///
if rad_right == 1, cluster(country_code)
est store afd1

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
if rad_right == 1, cluster(country_code)
est store afd2

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl ///
if rad_right == 1, cluster(country_code)
est store afd3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if rad_right == 1, cluster(country_code)
est store afd4

estimates stats afd1 afd2 afd3 afd4

* Then I run the analyses
regr reported_vote parl##c.performance ///
if rad_right == 1, cluster(country_code)

replace rad_right = 0 if partyyr == "AfD_GER13"

********
** Analyses for left-wing parties
********
** Non-parametric
* Using only centre-left parties
rdrobust reported_vote performance if ches_genlr <= 5.5 & ches_genlr >= 2 ///
& rad_right != 1 & ches_genlr != ., fuzzy (parl) vce(cluster country_code) all

* Using all left-wing parties
rdrobust reported_vote performance if ches_genlr <= 5.5 & rad_right != 1 ///
& ches_genlr != . , fuzzy (parl) vce(cluster country_code) all

** Parametric
* Using only centre-left parties
* First I check what the optimal number of polynomials is
regr reported_vote parl##c.performance ///
if ches_genlr <= 5.5 & ches_genlr >= 2 & rad_right != 1 & ches_genlr != ., cluster(country_code)
est store centreleft1

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
if ches_genlr <= 5.5 & ches_genlr >= 2 & rad_right != 1 & ches_genlr != ., cluster(country_code)
est store centreleft2

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl ///
if ches_genlr <= 5.5 & ches_genlr >= 2 & rad_right != 1 & ches_genlr != ., cluster(country_code)
est store centreleft3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if ches_genlr <= 5.5 & ches_genlr >= 2 & rad_right != 1 & ches_genlr != ., cluster(country_code)
est store centreleft4

estimates stats centreleft1 centreleft2 centreleft3 centreleft4

* Then I run the analyses
regr reported_vote parl c.performance##parl c.performance_sq##parl ///
if ches_genlr <= 5.5 & ches_genlr >= 2 & rad_right != 1 & ches_genlr != ., cluster(country_code)

* Using all left-wing parties
* First I check what the optimal number of polynomials is
regr reported_vote parl##c.performance ///
if ches_genlr <= 5.5 & rad_right != 1 & ches_genlr != ., cluster(country_code)
est store left1

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
if ches_genlr <= 5.5 & rad_right != 1 & ches_genlr != ., cluster(country_code)
est store left2

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl ///
if ches_genlr <= 5.5 & rad_right != 1 & ches_genlr != ., cluster(country_code)
est store left3

regr reported_vote parl c.performance##parl c.performance_sq##parl ///
c.performance3##parl c.performance4##parl ///
if ches_genlr <= 5.5 & rad_right != 1 & ches_genlr != ., cluster(country_code)
est store left4

estimates stats left1 left2 left3 left4

* Then I run the analyses
regr reported_vote parl##c.performance parl##c.performance_sq ///
if ches_genlr <= 5.5 & rad_right != 1 & ches_genlr != ., cluster(country_code)

***********
** Parametric approach with different number of polynomials
***********

* With one polynomial
regr reported_vote parl##c.performance if rad_right == 1, cluster(country_code)

* With two polynomials
regr reported_vote parl##c.performance c.performance_sq##parl if rad_right == 1, cluster(country_code)

* With three polynomials
regr reported_vote parl##c.performance parl##c.performance_sq ///
parl##c.performance3 if rad_right == 1, cluster(country_code)

* With four polynomials
regr reported_vote parl##c.performance parl##c.performance_sq ///
parl##c.performance3 parl##c.performance4 if rad_right == 1, cluster(country_code)

********************************************************************************
********************** STUDY 2: INDIVIDUAL-LEVEL EVIDENCE **********************
********************************************************************************

*******************
** Main Analyses **
*******************

use study2, clear

** Comparing rad right parties to all other parties **

* Column 1: Without controls
regr like public##parl##rad_right, cluster(ind_code)
margins public, at(parl=(0(1)1) rad_right=(0(1)1)) post

* Save results to Excel, to then plot

* Generate an excel file to store the findings
putexcel set study2.xlsx, replace

forvalues x = 1/8{
scalar group`x'_coef = el(r(table),1,`x')
scalar group`x'_se = el(r(table),2,`x')
}

forvalues x = 1/8{
putexcel A`x' = `x'
putexcel B`x' = group`x'_coef
putexcel C`x' = group`x'_se
}

* Variable for parl
putexcel D1 = 0
putexcel D2 = 0
putexcel D3 = 0
putexcel D4 = 0
putexcel D5 = 1
putexcel D6 = 1
putexcel D7 = 1
putexcel D8 = 1


* Variable for rad right
putexcel E1 = 0
putexcel E2 = 0
putexcel E3 = 1
putexcel E4 = 1
putexcel E5 = 0
putexcel E6 = 0
putexcel E7 = 1
putexcel E8 = 1

* Variable for public mode of interview
putexcel F1 = 0
putexcel F2 = 1
putexcel F3 = 0
putexcel F4 = 1
putexcel F5 = 0
putexcel F6 = 1
putexcel F7 = 0
putexcel F8 = 1

* Column 2: With controls 
regr like public##parl##rad_right age female_d househ_inc i.party_voted party_vote, cluster (ind_code)

* Column 3: Controlling for ideology instead of party voted for
regr like public##parl##rad_right age female_d househ_inc lrself party_vote, cluster (ind_code)

* Column 4: With demographic weights
regr like public##parl##rad_right [pweight = demweight], cluster(ind_code)

** Comparing rad right parties to centre-right only **
* Column 5: Without controls
regr like public##parl##rad_right if rad_right == 1 | centreright == 1, cluster (ind_code)

* Column 6: With controls 
regr like public##parl##rad_right age female_d househ_inc i.party_voted party_vote ///
if rad_right == 1 | centreright == 1, cluster (ind_code)

* Column 7: Controlling for ideology instead of party voted for
regr like public##parl##rad_right age female_d househ_inc lrself party_vote ///
if rad_right == 1 | centreright == 1, cluster (ind_code)

* Column 8: With demographic weights
regr like public##parl##rad_right [pweight = demweight] if rad_right == 1 | centreright == 1, cluster(ind_code)

********** Looking for heterogeneity based on previous election vote **********

regr like public##parl##left_voter if rad_right == 1, cluster(ind_code)
margins public, at(parl=(0(1)1) left_voter=(0(1)1)) post

* Save results to Excel, to then plot

* Generate an excel file to store the findings
putexcel set study2_left.xlsx, replace

forvalues x = 1/8{
scalar group`x'_coef = el(r(table),1,`x')
scalar group`x'_se = el(r(table),2,`x')
}

forvalues x = 1/8{
putexcel A`x' = `x'
putexcel B`x' = group`x'_coef
putexcel C`x' = group`x'_se
}

* Variable for parl
putexcel D1 = 0
putexcel D2 = 0
putexcel D3 = 0
putexcel D4 = 0
putexcel D5 = 1
putexcel D6 = 1
putexcel D7 = 1
putexcel D8 = 1


* Variable for left voters
putexcel E1 = 0
putexcel E2 = 0
putexcel E3 = 1
putexcel E4 = 1
putexcel E5 = 0
putexcel E6 = 0
putexcel E7 = 1
putexcel E8 = 1

* Variable for public mode of interview
putexcel F1 = 0
putexcel F2 = 1
putexcel F3 = 0
putexcel F4 = 1
putexcel F5 = 0
putexcel F6 = 1
putexcel F7 = 0
putexcel F8 = 1


********** Looking for heterogeneity based on college degree **********

regr like public##parl##college if rad_right == 1, cluster(ind_code)
margins public, at(parl=(0(1)1) college=(0(1)1)) post

* Save results to Excel, to then plot

* Generate an excel file to store the findings
putexcel set study2_college.xlsx, replace

forvalues x = 1/8{
scalar group`x'_coef = el(r(table),1,`x')
scalar group`x'_se = el(r(table),2,`x')
}

forvalues x = 1/8{
putexcel A`x' = `x'
putexcel B`x' = group`x'_coef
putexcel C`x' = group`x'_se
}

* Variable for parl
putexcel D1 = 0
putexcel D2 = 0
putexcel D3 = 0
putexcel D4 = 0
putexcel D5 = 1
putexcel D6 = 1
putexcel D7 = 1
putexcel D8 = 1


* Variable for college
putexcel E1 = 0
putexcel E2 = 0
putexcel E3 = 1
putexcel E4 = 1
putexcel E5 = 0
putexcel E6 = 0
putexcel E7 = 1
putexcel E8 = 1

* Variable for public mode of interview
putexcel F1 = 0
putexcel F2 = 1
putexcel F3 = 0
putexcel F4 = 1
putexcel F5 = 0
putexcel F6 = 1
putexcel F7 = 0
putexcel F8 = 1

********************************************************************************
************************** STUDY 3: CASE STUDY OF UKIP *************************
********************************************************************************

*************************************
** Analyses for National elections **
*************************************

use study3_generalelec, clear

xtset party_code time_period

* Standard 

xtreg reported_vote treat i.time_period, fe cluster(party_code)
regsave treat using ukip_graph_95.dta, ci replace addlabel (Model, Standard Model, order, 1, SE, Clustered)

* Comparing pre and post

xtreg reported_vote treat post_period, fe cluster (party_code)
regsave treat using ukip_graph_95.dta, ci append addlabel (Model, Comparing pre and post, order, 2, SE, Clustered)

* With demographic weights

xtreg reported_vote_w treat i.time_period, fe cluster (party_code)
regsave treat using ukip_graph_95.dta, ci append addlabel (Model, With demographic weights, order, 3, SE, Clustered)

* Placebo t-1 
xtreg reported_vote_lag treat i.time_period, fe cluster (party_code)
regsave treat using ukip_graph_95.dta, ci append addlabel (Model, Placebo t-1, order, 4, SE, Clustered)

******
** Replicating the analyses with 90% CI's, to append later one **
******
xtreg reported_vote treat i.time_period, fe cluster(party_code)
regsave treat using ukip_graph_90.dta, ci level(90) replace addlabel (Model, Standard Model, order, 1, SE, Clustered)

xtreg reported_vote treat post_period, fe cluster (party_code)
regsave treat using ukip_graph_90.dta, ci level(90) append addlabel (Model, Comparing pre and post, order, 2, SE, Clustered)

xtreg reported_vote_w treat i.time_period, fe cluster (party_code)
regsave treat using ukip_graph_90.dta, ci level(90) append addlabel (Model, With demographic weights, order, 3, SE, Clustered)

xtreg reported_vote_lag treat i.time_period, fe cluster (party_code)
regsave treat using ukip_graph_90.dta, ci level(90) append addlabel (Model, Placebo t-1, order, 4, SE, Clustered)

* Appending both datasets
use ukip_graph_95.dta, clear
rename ci_upper ci_upper_95
rename ci_lower ci_lower_95

append using ukip_graph_90

rename ci_upper ci_upper_90
rename ci_lower ci_lower_90

saveold ukip_graph.dta, version(12) replace

** Changing the control group **
use study3_generalelec, clear

xtset party_code time_period

* With all parties in the control group 

* Standard 

xtreg reported_vote treat i.time_period, fe cluster(party_code)
regsave treat using ukip_controls.dta, ci replace ///
addlabel (Model, Standard Model,  Control, All)

* Comparing pre and post

xtreg reported_vote treat post_period, fe cluster (party_code)
regsave treat using ukip_controls.dta, ci append ///
addlabel (Model, Comparing pre and post, Control, All)

* With demographic weights

xtreg reported_vote_w treat i.time_period, fe cluster (party_code)
regsave treat using ukip_controls.dta, ci append ///
addlabel (Model, With demographic weights, Control, All)

* With just one other party in the control group

* Note: UKIP is party code 1, so party codes 2 through 4 represent parties in the control group
forvalues x = 2/4{
* Standard 
xtreg reported_vote treat i.time_period if party_code == 1 | party_code == `x', fe cluster(party_code)
regsave treat using ukip_controls.dta, ci append ///
addlabel (Model, Standard Model,  Control, `x')

* Comparing pre and post
xtreg reported_vote treat post_period if party_code == 1 | party_code == `x', fe cluster (party_code)
regsave treat using ukip_controls.dta, ci append ///
addlabel (Model, Comparing pre and post, Control, `x')

* With demographic weights
xtreg reported_vote_w treat i.time_period if party_code == 1 | party_code == `x', fe cluster (party_code) 
regsave treat using ukip_controls.dta, ci append ///
addlabel (Model, With demographic weights, Control, `x')
}

* With two other parties in the control group
forvalues x = 2/4{
* Standard 
xtreg reported_vote treat i.time_period if party_code != `x', fe cluster(party_code)
regsave treat using ukip_controls.dta, ci append ///
addlabel (Model, Standard Model,  Control, Excluding `x')

* Comparing pre and post
xtreg reported_vote treat post_period if party_code != `x', fe cluster (party_code) 
regsave treat using ukip_controls.dta, ci append ///
addlabel (Model, Comparing pre and post, Control, Excluding `x')

* With demographic weights
xtreg reported_vote_w treat i.time_period if party_code != `x', fe cluster (party_code)
regsave treat using ukip_controls.dta, ci append ///
addlabel (Model, With demographic weights, Control, Excluding `x')
}

** Creating dataset for parallel trends graph

collapse (mean) meanrepvote = reported_vote (sd) sd = reported_vote (count) ///
n = party_code, by(evertreat time_period)

gen ci_upper = meanrepvote + (1.96 * sd) / sqrt(n)
gen ci_lower = meanrepvote - (1.96 * sd) / sqrt(n)

replace ci_upper = meanrepvote if ci_upper == .
replace ci_lower = meanrepvote if ci_lower == .

save partrends_genelec.dta, replace

* Checking for differences in the composition of UKIP voters in 2010 and 2015

use UKIP2010_2015, clear

* Standardizing the variables
egen gender_m = mean(gender)
egen gender_sd = sd(gender)
gen gender_std = (gender-gender_m) / gender_sd
sum gender_std

egen age_m = mean(age)
egen age_sd = sd(age)
gen age_std = (age-age_m) / age_sd
sum age_std

egen hinc_m = mean(hinc_d)
egen hinc_sd = sd(hinc_d)
gen hinc_std = (hinc_d-hinc_m) / hinc_sd
sum hinc_std

regr gender_std post_d, r
regsave post_d using ukip_graph_sociodem_95.dta, ci replace addlabel (Model, Gender (Standardized), order, 1)

regr age_std post_d, r
regsave post_d using ukip_graph_sociodem_95.dta, ci append addlabel (Model, Age (Standardized), order, 2)

regr hinc_std post_d, r
regsave post_d using ukip_graph_sociodem_95.dta, ci append addlabel (Model, Household Income (Standardized), order, 3)

* Replicating using 90% CI's 
regr gender_std post_d, r
regsave post_d using ukip_graph_sociodem_90.dta, ci level(90) replace addlabel (Model, Gender (Standardized), order, 1)

regr age_std post_d, r
regsave post_d using ukip_graph_sociodem_90.dta, ci level(90) append addlabel (Model, Age (Standardized), order, 2)

regr hinc_std post_d, r
regsave post_d using ukip_graph_sociodem_90.dta, ci level(90) append addlabel (Model, Household Income (Standardized), order, 3)

* Appending both datasets
use ukip_graph_sociodem_95.dta, clear
rename ci_upper ci_upper_95
rename ci_lower ci_lower_95

append using ukip_graph_sociodem_90

rename ci_upper ci_upper_90
rename ci_lower ci_lower_90

saveold ukip_graph_sociodem.dta, version(12) replace

*******************************
** Analyses for EP elections **
*******************************

use study3_epelec, clear

xtset party_code time_period

* Standard 

xtreg reported_vote treat i.time_period, fe cluster(party_code)
regsave treat using ukip_graph_ep_95.dta, ci replace addlabel (Model, Standard Model, order, 1)

* Comparing pre and post

xtreg reported_vote treat post_period, fe cluster (party_code)
estimates store ukip2
regsave treat using ukip_graph_ep_95.dta, ci append addlabel (Model, Comparing pre and post, order, 2)

* With demographic weights

xtreg reported_vote_w treat i.time_period, fe cluster (party_code)
estimates store ukip3
regsave treat using ukip_graph_ep_95.dta, ci append addlabel (Model, With demographic weights, order, 3)

* Placebo t-1 *

xtreg reported_vote_lag treat i.time_period, fe cluster (party_code)
estimates store ukip4
regsave treat using ukip_graph_ep_95.dta, ci append addlabel (Model, Placebo t-1, order, 4)


**********
** Replicating with 90% CI's, to append later on **
**********

* Standard 

xtreg reported_vote treat i.time_period, fe cluster(party_code)
regsave treat using ukip_graph_ep_90.dta, ci level(90) replace addlabel (Model, Standard Model, order, 1)

* Comparing pre and post

xtreg reported_vote treat post_period, fe cluster (party_code)
estimates store ukip2
regsave treat using ukip_graph_ep_90.dta, ci level(90) append addlabel (Model, Comparing pre and post, order, 2)

* With demographic weights

xtreg reported_vote_w treat i.time_period, fe cluster (party_code)
estimates store ukip3
regsave treat using ukip_graph_ep_90.dta, ci level(90) append addlabel (Model, With demographic weights, order, 3)

* Placebo t-1 *

xtreg reported_vote_lag treat i.time_period, fe cluster (party_code)
estimates store ukip4
regsave treat using ukip_graph_ep_90.dta, ci level(90) append addlabel (Model, Placebo t-1, order, 4)


** Appending both datasets
use ukip_graph_ep_95.dta, clear
rename ci_upper ci_upper_95
rename ci_lower ci_lower_95

append using ukip_graph_ep_90

rename ci_upper ci_upper_90
rename ci_lower ci_lower_90

saveold ukip_graph_ep.dta, version(12) replace

** Creating dataset for parallel trends graph

use study3_epelec, clear

collapse (mean) meanrepvote = reported_vote (sd) sd = reported_vote (count) ///
n = party_code, by(evertreat time_period)

gen ci_upper = meanrepvote + (1.96 * sd) / sqrt(n)
gen ci_lower = meanrepvote - (1.96 * sd) / sqrt(n)

replace ci_upper = meanrepvote if ci_upper == .
replace ci_lower = meanrepvote if ci_lower == .

save partrends_ep.dta, replace


